﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Hockey.UILayer.Controls
{
    /// <summary>
    /// Canvas subclass implementing centering arround canvas
    /// </summary>
    internal class CenterCanvas: Canvas
    {
        protected override Size MeasureOverride(Size constraint)
        {
            constraint = base.MeasureOverride(constraint);

            if (Children.Count == 1)
            {
                UIElement visual = this.GetVisualChild(0) as UIElement;
                visual.Measure(constraint);                
            }

            return constraint;
        }

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            arrangeSize = base.ArrangeOverride(arrangeSize);

            if (Children.Count == 1)
            {
                FrameworkElement visual = this.GetVisualChild(0) as FrameworkElement;

                double x = (arrangeSize.Width - visual.Width) / 2;
                double y = (arrangeSize.Height - visual.Height) / 2;

                visual.Arrange(new Rect(x, y, visual.ActualWidth, visual.ActualHeight));


                //Canvas.SetLeft(visual, x);
                //Canvas.SetTop(visual, y);
            }

            return arrangeSize;
        }
    }
}
